# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Contributor: Jürgen Pfeifer <juergen@familiepfeifer.de>
# Contributor: Ray Donnelly <mingw.android@gmail.com>
# Contributor: Diego Sogari <diego.sogari@gmail.com>

_realname=qhull
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
conflicts=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
replaces=("${MINGW_PACKAGE_PREFIX}-${_realname}-git")
pkgver=2020.2
pkgrel=2
pkgdesc="A general dimension code for computing convex hulls and related structures (mingw-w64)"
arch=('any')
url="http://www.qhull.org/"
license=('custom')
options=('strip' 'staticlibs')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja")
source=("${_realname}-${pkgver}.tar.gz"::"https://github.com/qhull/qhull/archive/${pkgver}.tar.gz"
        "01-fix-library-export.patch")
sha256sums=('59356b229b768e6e2b09a701448bfa222c37b797a84f87f864f97462d8dbc7c5'
            '86d6dd1d049c454ba5da964c875c9d0d60cf256c954703c816d20031a3b1f9c1')

prepare() {
  cd "${srcdir}/${_realname}-${pkgver}"
  patch -Np1 -i "${srcdir}/01-fix-library-export.patch"
}

build() {
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}" && cd "${srcdir}/build-${MINGW_CHOST}"

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake.exe \
    -G"Ninja" \
    -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
    -DCMAKE_BUILD_TYPE=Release \
    -DBUILD_STATIC_LIBS=ON \
    -DBUILD_SHARED_LIBS=ON \
    -DLINK_APPS_SHARED=ON \
    "../${_realname}-${pkgver}"

  ${MINGW_PREFIX}/bin/cmake.exe --build ./

  # Deprecated, required for GNU octave
  ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target libqhull
}

check() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  ${MINGW_PREFIX}/bin/ctest.exe ./ || true
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake.exe --build ./ --target install

  install -Dm644 libqhull.dll "${pkgdir}${MINGW_PREFIX}/bin/libqhull.dll"
  install -Dm644 libqhull.dll.a "${pkgdir}${MINGW_PREFIX}/lib/libqhull.dll.a"

  install -Dm644 "${srcdir}/${_realname}-${pkgver}/COPYING.txt" "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING.txt"
}
